from typing import Set


class URLManager:
    """
    url 管理器
    """

    def __init__(self) -> None:
        # 待爬取的 url 集合
        self._capture_urls = set()
        # 已爬取的 url 集合
        self._finished_urls = set()

    def has_url(self) -> bool:
        """
        判断待爬取集合，是否有 url
        """
        return len(self._capture_urls) != 0
    
    def get_url(self) -> str:
        """
        从待爬取集合中获取 url
        """
        url = self._capture_urls.pop()
        self._finished_urls.add(url)
        return url
    
    def add_url(self, url: str) -> None:
        """
        添加 url 到待爬取集合中
        """
        if url and url not in self._capture_urls and url not in self._finished_urls:
            self._capture_urls.add(url)

    def add_urls(self, urls: Set[str]) -> None:
        """
        批量添加 url 到待爬取集合中
        """
        for url in urls:
            self.add_url(url)
